package com.zzh.myapplication.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.Encrypt;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/4/14 17:53
 * 修改人：Gophy
 * 修改时间：16/4/14 17:53
 * 修改备注：
 */
public class ChangePayPasswdActivity extends BaseActivity {
    private Toolbar toolbar;
    private EditText edit_oldPassword, edit_newPassword, edit_agian_newPassword;
    private RelativeLayout setting_relativalayout;
    private Button certificate_submit_btn;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_change_pay_passwd);
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        JudgePayPasswordParams();
        setting_relativalayout = (RelativeLayout) findViewById(R.id.setting_relativalayout);
        edit_oldPassword = (EditText) findViewById(R.id.edit_oldPassword);
        edit_newPassword = (EditText) findViewById(R.id.edit_newPassword);
        edit_agian_newPassword = (EditText) findViewById(R.id.edit_agian_newPassword);
        certificate_submit_btn = (Button) findViewById(R.id.certificate_submit_btn);
        certificate_submit_btn.setEnabled(false);


    }

    public void JudgePayPasswordParams() {
        String phone = SPUtils.getString(this, "LoginInfo", "");
        JSONObject object = new JSONObject();
        try {
            object.put("mobile", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println("first info::" + object.toString());

        PublicRequest.doPostRequest(UrlValues.JUDGE_PAY_PASSWORD, handler, object, 200, 100);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    try {//未设置支付密码！
                        JSONObject object = new JSONObject(msg.obj.toString());
                        if (object.opt("returnCode").equals("20000")) {
                            ToolBarHelper.setNormalTitleAndBack(ChangePayPasswdActivity.this, toolbar, getString(R.string.pay_passwd_setting));
                            certificate_submit_btn.setEnabled(true);
                            certificate_submit_btn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ChangeAcctPassToFrontParams();
                                }
                            });
                        } else if (object.opt("returnCode").equals("10000")){
                            ToolBarHelper.setNormalTitleAndBack(ChangePayPasswdActivity.this, toolbar, getString(R.string.pay_passwd));
                            setting_relativalayout.setVisibility(View.VISIBLE);

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 100:
                    break;
                case 2000:
                    T.show(ChangePayPasswdActivity.this, "设置成功", 1);
                    break;
            }
        }
    };

    public void ChangeAcctPassToFrontParams() {
        String newPassword = edit_newPassword.getText().toString();
        String agian_newPassword = edit_agian_newPassword.getText().toString();
        if (newPassword.equals(agian_newPassword)) {
            JSONObject object = new JSONObject();
            String phone = SPUtils.getString(this, "LoginInfo", "");
            try {
                object.put("mobile", phone);
                object.put("passwordType", "02");
                // object.put("oldPassword", "");
                object.put("newPassword", Encrypt.getMD5(agian_newPassword));
                System.out.println("change info::" + object.toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
            PublicRequest.customMainRequestPost(this, UrlValues.CHANGE_ACCT_PASS_TO_FRONT, handler, object);
        }
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onClick(View view) {

    }
}
